import 'package:flutter/material.dart';

class ScaleAnimationShow extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new _ScaleAnimationRouteState();
  }
}

/// 缩放动画
class _ScaleAnimationRouteState extends State
    with SingleTickerProviderStateMixin {
  Animation<double> animation;
  AnimationController controller;

  @override
  void initState() {
    super.initState();
    controller =
    new AnimationController(duration: Duration(seconds: 3), vsync: this);
    animation = new Tween(begin: 0.0, end: 300.0).animate(controller)
      ..addListener(() {
        setState(() {});
      })
      ..addStatusListener((status) {
        if (status == AnimationStatus.completed) {
          controller.reverse();
        } else if (status == AnimationStatus.dismissed) {
          controller.forward();
        }
      });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: new Text("缩放动画"),
        ),
        body: Center(
            child: Column(
              children: <Widget>[
                RaisedButton(
                  child: Text("开始动画"),
                  onPressed: () {
                    //controller.reset();
                    controller.forward();
                  },
                ),
                GrowTransition(
                  child: ClipOval(
                    child: Image.asset(
                      "lib/images/yinsuwan.jpeg",
                      fit: BoxFit.cover,
                    ),
                  ),
                  animation: animation,
                )
              ],
            )));
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
}

class GrowTransition extends StatelessWidget {
  final Widget child;
  final Animation animation;

  GrowTransition({this.child, this.animation});

  @override
  Widget build(BuildContext context) {
    return new Center(
      child: new AnimatedBuilder(
        animation: animation,
        builder: (context, child) {
          return Container(
            height: animation.value,
            width: animation.value,
            child: child,
          );
        },
        child: child,
      ),
    );
  }
}
